﻿using Admin.CommonLib.Models.Account;
using Admin.RazorLib.Services;
using Microsoft.AspNetCore.Components;
using Radzen;
using Swimj.RazorLib.Caches;
using System.Diagnostics.CodeAnalysis;

namespace Client.BlazorWasmApp.Pages
{
    public partial class Login
    {
        private LoginRequestDto _loginModel = new();

        [Inject]
        [NotNull]
        public IAccountService? AccountService { get; set; }

        [Inject]
        [NotNull]
        public ISecureCache? SecureCache { get; set; }

        [Inject]
        [NotNull]
        protected NavigationManager? UriHelper { get; set; }

        async Task OnSubmit(LoginRequestDto model)
        {
            var result = await AccountService.Login(_loginModel);
            if (result != null)
            {
                if (result.Succeeded)
                {

                    await SecureCache.SetAsync("authToken", result.Data?.Token!);
                    await SecureCache.SetAsync("refreshToken", result.Data?.RefreshToken!);

                    UriHelper.NavigateTo("/", true);
                }
                else
                {

                }
            }
        }

        void OnInvalidSubmit(FormInvalidSubmitEventArgs args)
        {

        }
    }
}
